package kin.system.util;

import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;




public class ValueOfType {
public static Object covertor(Class cls,String fieldName,String value){
	try {
		Field field=cls.getDeclaredField(fieldName);
		String fieldTypeName = field.getType().getName();
		if (fieldTypeName.equals("int")
				|| fieldTypeName.equals("java.lang.Integer")) {
			return value != null && !"".equals(value) ? Integer
					.parseInt(value) : 0;
		} else if (fieldTypeName.equals("long")
				|| fieldTypeName.equals("java.lang.Long")) {
			return value != null && value != "null" && !"".equals(value) ? Long
					.parseLong(value) : null;
		} else if (fieldTypeName.equals("float")
				|| fieldTypeName.equals("java.lang.Float")) {
			return value != null && value != "null" && !"".equals(value) ? Float
					.parseFloat(value) : null;
		} else if (fieldTypeName.equals("double")
				|| fieldTypeName.equals("java.lang.Double")) {
			return value != null && value != "null" && !"".equals(value) ? Double
					.parseDouble(value) : null;
		} else if (fieldTypeName.equals("boolean")
				|| fieldTypeName.equals("java.lang.Boolean")) {
			return value != null && value != "null" && !"".equals(value) ? Boolean
					.parseBoolean(value) : null;
		} else if (fieldTypeName.equals("java.util.Date")) {
			String s = null;
			System.out.println("value:"+value);
			if(value.contains("T")){
			s=value.split("T")[0];
			//s.replaceAll("-","/");
			}else{
				s=value;
			}
			System.out.println("date1:"+s);
			/*if (value.length() > 11) {
				
				ov = value != null && value != "null" && !"".equals(value) ? new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss").parse(value) : null;
			}*/ 
			try {
				return s != null && s != "null" && !"".equals(s) ? new SimpleDateFormat(
							"yyyy-MM-dd").parse(s) : null;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} else if (fieldTypeName.equals("java.lang.String")) {
			return (String) value;
		}
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return value;
	
}

public static  void setValue(Object o,String fieldName,String value){
	try {
		Field f= o.getClass().getDeclaredField(fieldName);
		if(f!=null){
			Object manager=ApplicationContextHelper.getBean(fieldName+"ManagerIMP");
			Object obj=manager.getClass().getMethod("load", new Class[] { Serializable.class }).invoke(manager, Integer.parseInt(value));
			f.setAccessible(true);
			f.set(o, obj);
		}
	} catch (SecurityException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchFieldException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NumberFormatException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalArgumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (InvocationTargetException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
}
}
